import request from '@/api/request';
import { AxiosPromise } from 'axios';
import { BlogQuery, BlogQueryParam, BlogInfo } from './types';

export function getBlogPage(data: BlogQuery): AxiosPromise<BlogInfo> {
  return request({
    url: '/api/v1/blogs/page',
    method: 'post',
    data
  });
}

/**
 * 博客卡片分页列表
 *
 * @param queryParams
 */
export function pageBlogCards(queryParams: BlogQueryParam): AxiosPromise<BlogInfo> {
  return request({
    url: '/api/v1/blogs/cards/page',
    method: 'get',
    params: queryParams
  });
}

export function getBlogDetail(blogId: string): AxiosPromise<BlogInfo> {
  return request({
    url: `/api/v1/blogs/detail/${blogId}`,
    method: 'get'
  });
}

/**
 * 浏览博客
 * @param blogId 博客id
 * @returns
 */
export function browseBlog(blogId: string): AxiosPromise<BlogInfo> {
  return request({
    url: `/api/v1/blogs/browse/${blogId}`,
    method: 'get'
  });
}

export function saveBlogApi(data: any) {
  return request({
    url: '/api/v1/blogs',
    method: 'post',
    data
  });
}

export function updateBlogApi(data: any) {
  return request({
    url: '/api/v1/blogs',
    method: 'put',
    data
  });
}

export function deleteBlog(data: any) {
  return request({
    url: '/api/v1/blogs',
    method: 'delete',
    data
  });
}

export function getLatestBlog(): AxiosPromise<BlogInfo> {
  return request({
    url: '/api/v1/blogs/latest',
    method: 'get'
  });
}

/**
 * 获取当前用户最近编辑的草稿
 * @param limit 限制数量，默认5个
 */
export function getRecentDrafts(limit?: number): AxiosPromise<BlogInfo[]> {
  return request({
    url: '/api/v1/blogs/drafts/recent',
    method: 'get',
    params: limit ? { limit } : {}
  });
}
